import {Component, Input, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {Zero3AlertLg} from '../global/zero3-alert-lg/zero3-alert-lg.component';
import {environment} from '../../../environments/environment';
import {SysUserCondition} from '../../module/system-management/sys-user/sys-user.type';
import {UrlApi} from '../index';
import {HttpClient, HttpHeaders} from '@angular/common/http';

@Component({
    selector: 'app-zero3-layout-navbar',
    templateUrl: './zero3-layout-navbar.component.html',
    styleUrls: ['./zero3-layout-navbar.component.css']
})
export class Zero3LayoutNavbarComponent implements OnInit {

    static RES_STATUS = -1;

    @Input('nav')
    nav: { upper: any, self: Zero3LayoutNavbarComponent };

    Navbar = Zero3LayoutNavbarComponent;

    fa = fa;

    navbar = {
        home: {
            name: `Home ${environment.production}`,
            href: (event) => {
                this.router.navigate(['/']);
            }
        },
        module: {
            name: 'module'
        },
        menu: {
            name: 'menu',
            href: (event) => {
                this.router.navigate(['/template-example/tem-style1/list', {moduleId: 'template-example', menuId: 'tem-style1'}]);
            }
        },
        func: {
            name: 'func',
            logout: {
                req: (event) => {
                    const headers = new HttpHeaders();
                    headers.set('Content-Type', 'application/json;charset=UTF-8');
                    const httpOptions = {
                        headers: headers
                    };
                    console.log(UrlApi.sysUser.logout);
                    this.http.put(UrlApi.sysUser.logout, new SysUserCondition(), httpOptions).subscribe({
                        next: x => console.log('Observer got a next value: ' + x),
                        error: err => console.error('Observer got an error: ' + err),
                        complete: () => {
                            console.log('Observer got a complete notification');
                            this.router.navigate(['/login']);
                            // this.router.navigate([UrlRou.sysUser.login]);
                        },
                    });
                }
            }
        },
    };

    constructor(private router: Router, private http: HttpClient) {
    }

    ngOnInit() {
        this.nav.self = this;
        this.navbar.func.name = this.nav.upper.funcName;
    }

    click400(event) {
        Zero3AlertLg.reShow();
    }

}
